<script setup>
import { ref } from 'vue'
import ChannelSelect from './ChannelSelect.vue'
import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { artPublishService } from '@/api/article'
import { ElMessage } from 'element-plus'
import { nextTick } from 'vue'

// 抽屉显示隐藏
const drawer = ref(false)
const emit = defineEmits(['success'])
const editorRef = ref()

const fromDefulatData = {
  id: '', // id
  title: '', // 标题
  cate_id: '', // 分类id
  content: '', //内容
  cover_img: '', // 文章封面图像 file
  state: '' // 状态 (已发布/草稿)
}
const formModel = ref({ ...fromDefulatData })

// 图片上传
const imageUrl = ref('')
// 预览图片
const onSelectFile = (uploadFile) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  // 将图片存入formModel中便于提交
  formModel.value.cover_img = uploadFile.raw
}

const open = async (row) => {
  drawer.value = true
  if (row.id) {
    // 请求接口获取需要填充的表单数据
    console.log('请求接口获取需要填充的表单数据')
  } else {
    // 添加操作需要清空表单
    formModel.value = { ...fromDefulatData }
    // 清空图片
    imageUrl.value = ''
    await nextTick(() => {
      editorRef.value.setHTML('')
    })
  }
}

// 发布文章
const onPublish = async (state) => {
  formModel.value.state = state
  // 转换成 formData 对象
  const fd = new FormData()
  for (let key in formModel.value) {
    fd.append(key, formModel.value[key])
  }

  // 如果id存在则是编辑
  if (formModel.value.id) {
    console.log(123)
  } else {
    await artPublishService(fd)
    ElMessage.success('发布成功')
    drawer.value = false // 隐藏抽屉
    // 提交时间告诉父组件提交成功
    emit('success', 'add')
  }
}

defineExpose({
  open
})
</script>

<template>
  <el-drawer
    v-model="drawer"
    :title="formModel.id ? '编辑文章' : '添加文章'"
    direction="rtl"
    size="50%"
  >
    <!-- 发表文章表单 -->
    <el-form :model="formModel" ref="formRef" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <channel-select
          v-model="formModel.cate_id"
          width="100%"
        ></channel-select>
      </el-form-item>
      <el-form-item label="文章封面" prop="cover_img">
        <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :auto-upload="false"
          :on-change="onSelectFile"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <div class="editor">
          <quill-editor
            theme="snow"
            v-model:content="formModel.content"
            content-type="html"
            ref="editorRef"
          ></quill-editor>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onPublish('已发布')">发布</el-button>
        <el-button type="info" @click="onPublish('草稿')">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }
    .el-upload:hover {
      border-color: var(--el-color-primary);
    }
    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 170px;
  }
}
</style>
